import pytest
from py.xml import html


def pytest_html_report_title(report):
    report.title = "演示用例报告"


def pytest_metadata(metadata):
    metadata.pop('JAVA_HOME')
    metadata.pop('Packages')
    metadata.pop('Plugins')
    metadata['apirunner'] = '0.1.0'


def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p('项目名称: 演示项目')])
    prefix.extend([html.p()])
    prefix.extend([html.p('测试人员: 指尖')])
    prefix.extend([html.p()])


def pytest_html_results_table_header(cells):
    cells.insert(2, html.th("Description", class_="sortable description", col="description"))
    cells.pop(-1)


def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.pop(-1)


def pytest_html_results_table_html(report, data):
    """去除 pass 的测试用例的日志捕捉"""
    # if report.passed:
    #     del data[:]
    #     data.append(html.div('未捕获日志输出', class_="empty log"))
    pass


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    setattr(report, "duration_formatter", "%H:%M:%S.%f")

    report.description = str(item.function.__doc__)


def pytest_collection_modifyitems(items):
    """下面是用于调试单条测试用例的时候在窗口显示中文编码，（注意：如果需要生成报告在 html中，
    需要把以下代码注释，两者留其一，目前未找到原因）
    如果需要在 pytest-html 中显示中文编码，需要把以下的代码注释掉"""
    # for item in items:
    #     item.name = item.name.encode("utf-8").decode("unicode_escape")
    #     item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")
    pass
